package databaseConnect;

import java.util.Vector;

import datatypes.CommonConstants;
import datatypes.MetricObject;


public class SimulationExecutionWriteProcessor {

	public static int setUUID(int simulationID) {
		
		java.sql.Time timestamp = new java.sql.Time((new java.util.Date()).getTime());
		java.sql.Date datestamp = new java.sql.Date((new java.util.Date()).getTime());
		
		String UUID = SimulationExecutionRequestProcessor.getUSID();
		if (UUID == null){
			return -1;
		}
		int UUID_int = Integer.parseInt(UUID);
		AccessConnect.executeQuery("INSERT INTO "+ CommonConstants.simulationExecutionTable +" VALUES ('" + UUID + "', '" + simulationID+ "', '" + datestamp + "', '" + timestamp + "' )");
		return UUID_int;
	}

	public static void writeMetricContent (int USID, String simID, MetricObject metric){
		java.sql.Time timestamp = new java.sql.Time((new java.util.Date()).getTime());
		java.sql.Date datestamp = new java.sql.Date((new java.util.Date()).getTime());
		String query = "INSERT INTO "+CommonConstants.simulationResultTable+" (USID, simID, datest, timest, TypeOfInformation, scenID, TimeStep, DiscoveryMechanism, MetricName" ;
		query = query + getColumnNames(metric.getContent());
		query = query 	+ ") VALUES ('" 
						+ USID + "', '" 
						+ simID + "', '" 
						+ datestamp + "', '" 
						+ timestamp + "', '"
						+ metric.getMetricType() + "',  '"
						+ metric.getContent().get(1) + "', '" //ScenarioID
						+ metric.getTimeStep() + "', '"
						+ metric.getDiscMech() + "', '"
						+ metric.getMetricName() + "'";
		query = query + getValues(metric.getContent());
		query = query + ")";
		AccessConnect.executeQuery(query);		
	}

	@SuppressWarnings("unchecked")
	private static String getColumnNames(Vector<String> content) {
		String tmpReturn = new String();
		if (content != null && content.size() > 3){
			Vector<String> contentClone = (Vector<String>) content.clone();
			contentClone.remove(contentClone.size()-1);
			contentClone.remove(0);
			contentClone.remove(0);	
			for (int i = 1 ; i <= contentClone.size(); i++){
					tmpReturn = tmpReturn + ", " + "Parameter" + i;
			}
			return tmpReturn;
		} else {
			return "";
		}
	}

	@SuppressWarnings("unchecked")
	private static String getValues(Vector<String> content) {
		String tmpReturn = new String();
		if (content != null && content.size() > 3){
			Vector<String> contentClone = (Vector<String>) content.clone();
			contentClone.remove(contentClone.size()-1);
			contentClone.remove(0);
			contentClone.remove(0);	
			for (int i = 0 ; i < contentClone.size(); i++){
				tmpReturn = tmpReturn + ", '" + contentClone.get(i) + "'"; 
			}
			return tmpReturn;
		} else {
			return "";
		}
	}
	
}
